
#include "Soa.h"


//constructor
Soa::Soa()
{
	requests = Queue<unsigned char>();
	registeredServices = Vector<Service*>(); 
	currentService = NULL;
}

//adds a service request to the requested services
void Soa::addRequest(unsigned char s)
{
	requests.push(s);
}
//runs the next requested services until finished
//current service is not finished continues running that service to completion
void Soa::run()
{
	if(currentService == NULL && requests.empty() == 1) //nothing to run
		return;
	else if(currentService == NULL && requests.empty() != 1) // add service to run
		currentService = registeredServices[
					requests.pop()];
	
	//check if the current Service is finished
	if(currentService->finished == true) //add set next service to run
	{
		if(requestedServices.empty() != 1)
		{
			//stop the finished service
			currentService->stop();
			//get next service.
			currentSevice = registeredServices[
					requests.pop()];
			//start up the service.
			currentService->start();
			
		}
		else
		{
			//requested services is empty stop
			//finished service
			currentService->stop();
			currentService = NULL;
		}
	}
	else //current is not finished so keep running it
	{
		currentService->run();
	}

}	


